Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wikidata] Fix ISNI crashing and WP parsing #103

Merged
merged 6 commits into from
Nov 26, 2024
Merged

Conversation

jesus2099
Copy link
Contributor

@jesus2099 jesus2099 commented Oct 24, 2024

Crash bugs (2) reported and pin-pointed by DenizC and diskotechjam in
https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099


  • WD is Wikidata
  • WP is Wikipedia

fillISNI function crash

Uncaught TypeError: Cannot read properties of undefined (reading 'parentElement')

edit-artist.isni_codes-template no longer exists in react version of artist edit page.

fillFormFromISNI function crash

Uncaught TypeError: Cannot read properties of null (reading 'length')

When regex does not match, it is null.

Fill ISNI code from WD

This does not work on the new react version of the artist edit page.

Reported by @zas

Fetch WD page from WP URL

It does no longer find WD page from WP URL.
Probably WP pages have changed.

Reported by diskotechjam


Bonus: Use only UTC time functions everywhere, for consistency

fillISNI function
-----------------

> Uncaught TypeError: Cannot read properties of undefined (reading 'parentElement')

edit-artist.isni_codes-template apparently no longer exists.


fillFormFromISNI function
-------------------------

> Uncaught TypeError: Cannot read properties of null (reading 'length')

When regex does not match, it is null.

---

Bugs reported by DenizC and diskotechjam in
https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099

---

Bonus: Use only UTC time functions everywhere, for consistency
@zas
Copy link

zas commented Nov 6, 2024

When an ISNI code is already there, it is now trying to add a new.

@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 6, 2024

Thanks @zas for testing.
Isn't it supposed to add ISNI to a new field when there is already one?
Can't artists have several ISNI, in practice, like they can have several IPI, like recordings can have several ISRC and like works can have several ISWC?

❌ BTW I notice that I have indeed removed the 2 ways of systematic crashing the script but ISNI is no longer set at all.

❌ For example I have an artist with no ISRC and the script just adds a second empty ISNI field.

So I will fix my fix.

But please advise what should be done when:

  • Artist already has a different ISNI
  • Artist already has the same ISNI
  • Artist has no ISNI

@zas
Copy link

zas commented Nov 6, 2024

When an ISNI code is already there, it is now trying to add a new

What I meant is it tries to add the same ISNI as it was new (so it displays it as a new field, but doesn't actually add it).

About various cases:

  • Artist already has a different ISNI
    

Perhaps just warn, but don't add the new one, it might be an error, and we don't know where

  • Artist already has the same ISNI
    

Do nothing

  • Artist has no ISNI
    

Add the one from WD (if any, of course)

MBS new react-hydrate system makes it so that script added ISNI code
will not visually persist if you click "Add ISNI code" button afterwards
or things like that.

I tested these known react-hydrate-resist tricks
but they unfortunately did not work for ISNI codes:

https://github.com/jesus2099/konami-command/blob/a1dd018d84cecee965943dda19617a6807dcedc4/lib/SUPER.js#L163-L172

d4c8183
@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 7, 2024

I have fixed the WP URL handling (09f3a01) and the actual filling of ISNI code field from WD (582de28), in this same PR proposal.

The only problem that remains:

MBS new react-hydrate system makes it so that script added ISNI code
will not visually persist if you click "Add ISNI code" button afterwards
or things like that.

I tested these known react-hydrate-resist tricks
but they unfortunately did not work for ISNI codes:

https://github.com/jesus2099/konami-command/blob/a1dd018d84cecee965943dda19617a6807dcedc4/lib/SUPER.js#L163-L172
d4c8183

@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 7, 2024

I put the PR on hold as I will try my react-hydrate workaround once more, tomorrow.
I think I made a mistake when I tried it on ISNI code!

No no, I did already test it well in 582de28 and this react-hydrate workaround doesn't work for ISNI codes, indeed.
I reactivate the PR and there is that slight issue (on some browsers, not on Android Kiwi):
Newly inserted ISNI code (automatically from WD) visually disappears, if you click the Add ISNI code button without having interacted with the new code manually beforehand.

Last update

Problem fixed by c298541 thanks to https://nattaylor.com/blog/2022/userscripts-and-reactjs-forms/
The trick was to let the input event bubbling up!

bubbles: true

@jesus2099 jesus2099 marked this pull request as draft November 7, 2024 01:45
@jesus2099 jesus2099 changed the title [wikidata] Fix ISNI functions crash [wikidata] Fix ISNI crashing and WP parsing Nov 7, 2024
@jesus2099 jesus2099 marked this pull request as ready for review November 7, 2024 09:59
Fixes the small hindrance of visual disappearance of ISNI,
when clicking the new ISNI button
Hindrance described in 582de28

The trick was to let the input event BUBBLING UP!
https://nattaylor.com/blog/2022/userscripts-and-reactjs-forms/
@jesus2099 jesus2099 closed this Nov 7, 2024
@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 7, 2024

Problem fixed by c298541 thanks to https://nattaylor.com/blog/2022/userscripts-and-reactjs-forms/
The trick was to let the input event bubbling up!

bubbles: true

@jesus2099 jesus2099 reopened this Nov 7, 2024
@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 9, 2024

Hi @Jerome-Roy / @loujine
This PR is now ready, I think.
It makes the userscript working, again.
I hope you’re all right, in your cold country.

@loujine
Copy link
Owner

loujine commented Nov 25, 2024

Not so cold when volcanoes wake up :)

Thanks for your PR, I'll try to take a look this week. I merged some simpler pull requests tonight, ping me if I broke something

@jesus2099
Copy link
Contributor Author

jesus2099 commented Nov 25, 2024

I tried to analyse the merge conflict, please tell me if it's OK for you, @loujine. :)

Remaining conflict compare

Repo master branch:

    const isniBlock = document.getElementById(
        'add-isni-code').parentElement.parentElement;
    const fields = isniBlock.getElementsByTagName('input');
    for (const input of fields) {
        existing_isni.push(input.value.split(' ').join(''));
    }
    existing_isni.splice(0, 1); // skip template
    if (existing_isni.includes(isni.split(' ').join(''))) {
        return;
    }
    if (existing_isni.length === 1 && existing_isni[0] === '') {
        document.getElementsByName('edit-artist.isni_codes.0')[0].value = isni;

Repo master branch:

    const isni_fields = document.querySelectorAll('input[name^="edit-artist.isni_codes."]');
    for (const input of isni_fields) {
        if (input.value) {
            existing_isni.push(input.value.split(' ').join(''));
        }
    }
    if (existing_isni.length === 0) {
        (Object.getOwnPropertyDescriptor(Object.getPrototypeOf(isni_fields[0]), 'value').set).call(isni_fields[0], isni);
        isni_fields[0].dispatchEvent(new Event('input', {bubbles: true}));
        $('#newFields').append(
            $('<dt>', {'text': 'New ISNI code added:'})
        ).append(
            $('<dd>', {'text': isni}).css('color', 'green')
        );

Break out

ISNI fields selector

master branch:

    const isniBlock = document.getElementById(
        'add-isni-code').parentElement.parentElement;
    const fields = isniBlock.getElementsByTagName('input');

isniBlock is not used anywhere else, later.

This PR branch:

    const isni_fields = document.querySelectorAll('input[name^="edit-artist.isni_codes."]');

These are doing the same thing, tell me if it's OK to keep this version of the ISNI selector fix, @loujine?

Template

Template no longer exists and we should remove the skip:

master branch:

    existing_isni.splice(0, 1); // skip template

Removed by this PR.

Inform the user of already existing ISNI

master branch does not inform the user:

    if (existing_isni.includes(isni.split(' ').join(''))) {
        return;
    }

This PR informed when same ISNI is already set.

Inform the user of new ISNI set

This PR branch:

        $('#newFields').append(
            $('<dt>', {'text': 'New ISNI code added:'})
        ).append(
            $('<dd>', {'text': isni}).css('color', 'green')
        );

This PR now informs when the new ISNI is being set, like for other attribute changes.

Setting value so that it is not hidden later by react-hydrate

Repo master branch:

        document.getElementsByName('edit-artist.isni_codes.0')[0].value = isni;

Repo master branch:

        (Object.getOwnPropertyDescriptor(Object.getPrototypeOf(isni_fields[0]), 'value').set).call(isni_fields[0], isni);
        isni_fields[0].dispatchEvent(new Event('input', {bubbles: true}));

Now that pages are using react, it is needed to prevent display bugs (disappearing ISNI when giving Add ISNI button), this way.

@jesus2099
Copy link
Contributor Author

I solved conflict, keeping my ISNI fields selector.
I hope it's good for you, otherwise, I can amend last commit. :)

@jesus2099
Copy link
Contributor Author

I'm glad to hear from you, again! :)

@loujine
Copy link
Owner

loujine commented Nov 26, 2024

I tested quickly, looks good, thanks so much
The hydration fix looks like the one I needed in the common script last year

@loujine loujine merged commit 00f5b76 into loujine:master Nov 26, 2024
2 checks passed
loujine pushed a commit that referenced this pull request Nov 26, 2024
Crash bugs (2) reported and pin-pointed by DenizC and diskotechjam in

https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099

---

- WD is Wikidata
- WP is Wikipedia

---

fillISNI function crash
-----------------------

> Uncaught TypeError: Cannot read properties of undefined (reading
'parentElement')

`edit-artist.isni_codes-template` no longer exists in react version of
artist edit page.

fillFormFromISNI function crash
-------------------------------

> Uncaught TypeError: Cannot read properties of null (reading 'length')

When regex does not match, it is `null`.

Fill ISNI code from WD
----------------------

This does not work on the new react version of the artist edit page.

Reported by @zas

Fetch WD page from WP URL
-------------------------

It does no longer find WD page from WP URL.
Probably WP pages have changed.

Reported by diskotechjam

---

Bonus: Use only UTC time functions everywhere, for consistency
loujine pushed a commit that referenced this pull request Nov 26, 2024
Crash bugs (2) reported and pin-pointed by DenizC and diskotechjam in

https://community.metabrainz.org/t/user-script-to-load-artist-data-from-wikipedia/86338/38?u=jesus2099

---

- WD is Wikidata
- WP is Wikipedia

---

fillISNI function crash
-----------------------

> Uncaught TypeError: Cannot read properties of undefined (reading
'parentElement')

`edit-artist.isni_codes-template` no longer exists in react version of
artist edit page.

fillFormFromISNI function crash
-------------------------------

> Uncaught TypeError: Cannot read properties of null (reading 'length')

When regex does not match, it is `null`.

Fill ISNI code from WD
----------------------

This does not work on the new react version of the artist edit page.

Reported by @zas

Fetch WD page from WP URL
-------------------------

It does no longer find WD page from WP URL.
Probably WP pages have changed.

Reported by diskotechjam

---

Bonus: Use only UTC time functions everywhere, for consistency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants